package com.splic.api.controller.product;

import com.github.pagehelper.PageInfo;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.domain.ResponseObj;
import com.splic.common.exception.BusinessException;
import com.splic.common.utils.ShiroUtils;
import com.splic.common.utils.StringUtils;
import com.splic.product.domain.HotSearchKeyword;
import com.splic.product.domain.UserSearchKeywordRecord;
import com.splic.product.service.HotsearchKeywordService;
import com.splic.product.service.UserSearchKeywordRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 搜索关键字
 */
@RestController
@RequestMapping("api/keyword")
@Slf4j
public class ApiKeyWordController extends BaseController {

    @Autowired
    private UserSearchKeywordRecordService userSearchKeywordRecordServiceImpl;

    @Autowired
    private HotsearchKeywordService hotsearchKeywordServiceImpl;
    /**
     * 保存关键词
     *
     * @return
     */
    @PostMapping("/saveKeyword")
    public ResponseObj saveKeyword(@RequestBody UserSearchKeywordRecord userSearchKeywordRecord) {
        try {
            if(StringUtils.isEmpty(userSearchKeywordRecord.getKeyword())){
                return ResponseObj.ERROR("参数为空");
            }
            userSearchKeywordRecord.setUserId(ShiroUtils.getUserId());
            int count = userSearchKeywordRecordServiceImpl.insert(userSearchKeywordRecord);
            if(count <= 0){
                throw new BusinessException("用户搜索关键字保存失败");
            }
            return ResponseObj.SUCCESS();
        }catch (Exception e){
            log.error("用户搜索关键字保存失败:{}",e);
            return ResponseObj.ERROR("用户搜索关键字保存失败");
        }
    }


    /**
     * 查询热门关键词
     *
     * @return
     */
    @GetMapping("/getHotKeywordList")
    public ResponseObj<PageInfo<HotSearchKeyword>> getHotKeywordList() {
        try{
            startPage();
            List<HotSearchKeyword> list = hotsearchKeywordServiceImpl.selectPageList(null);
            return ResponseObj.SUCCESS(new PageInfo(list));
        }catch (Exception e){
            log.error("查询热门关键词失败:{}",e);
            return ResponseObj.ERROR("查询热门关键词失败");
        }
    }
}
